package game.module.nn.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.nn.bean.NnRoom;
import game.module.nn.logic.NnDataManager;
import game.module.offline.bean.PlayerOfflineCache;
import game.module.offline.logic.PlayerOfflineManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageNn.C2SNnBackRoomHall;
import lua.LuaMessageNn.NNMemberInfo;
import lua.LuaMessageNn.S2CNNJoinRoom;
import lua.LuaMessageNn.S2CNnBackRoomHall;

@MsgCodeAnn(msgcode = C2SNnBackRoomHall.id, accessLimit = 500)

public class NnBackRoomProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(NnBackRoomProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		int playerId = playingRole.getId();
		logger.info("nn back hall room,playerId={}", playerId);
		// 是否在房间中
		boolean isInRoom = NnDataManager.getInstance().isPlayerInRoom(playerId);
		if (!isInRoom) {
			S2ErrorCode respMsg = new S2ErrorCode(106);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 房间是否存在
		NnRoom nnroom = NnDataManager.getInstance().getNnRoomByPlayerId(playerId);
		if (nnroom == null) {
			S2ErrorCode respMsg = new S2ErrorCode(104);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// ret
		String room_id = nnroom.getUuid();
		S2CNnBackRoomHall respMsg = new S2CNnBackRoomHall();
		respMsg.hall_info = new S2CNNJoinRoom();
		respMsg.hall_info.room_id = room_id;
		respMsg.hall_info.round_count = nnroom.getRoundCount();
		respMsg.hall_info.members = new NNMemberInfo[nnroom.getPlayerList().size()];
		respMsg.hall_info.prevent_cheat = nnroom.getPrevent_cheat();
		respMsg.hall_info.roomFeeType = nnroom.getRoomFeeType();
		List<Integer> playerIdList = nnroom.getPlayerList();
		int i = 0;
		for (Integer aPlayerId : playerIdList) {
			PlayerOfflineCache poc = PlayerOfflineManager.getInstance().getPlayerOfflineCache(aPlayerId);
			respMsg.hall_info.members[i++] = new NNMemberInfo(aPlayerId, poc.getHeadimg(), poc.getLevel(), poc.getSex().shortValue(), poc.getName());
		}
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}

}
